package com.yehui.alibaba.controller;

import com.yehui.alibaba.service.at.ATService;
import com.yehui.alibaba.service.at.OrderService;
import com.yehui.alibaba.service.tcc.TCCService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 业徽
 * @date 2021/8/4
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private ATService atService;

    @Resource
    private TCCService tccService;

    @PostMapping("/create/at")
    @ResponseBody
    public Integer createOrder(@RequestParam("userId") Long userId,
                               @RequestParam("productId") Long productId,
                               @RequestParam("price") Integer price) throws Exception {
        logger.info("[createOrder] 收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price);
        return atService.createOrder(userId, productId, price);
    }

    @PostMapping("/create/tcc")
    @ResponseBody
    public Integer createWithTCC(@RequestParam("userId") Long userId,
                               @RequestParam("productId") Long productId,
                               @RequestParam("price") Integer price) throws Exception {
        logger.info("[createOrder] 收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price);
        return tccService.createOrder(userId, productId, price);
    }


}
